﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;
using System.Configuration;
using System.Web.UI.HtmlControls;
using FileUploadUNCShare;
using System.IO;
using System.Web.UI.WebControls;
/// <summary>
/// Summary description for Common
/// </summary>
public class Common
{

    //
    // TODO: Add constructor logic here
    //

    public const System.String PAGESIZE = "25";

    public int PageSize = 25;

    #region Status Message
    /// <summary>
    /// Shows the status message.
    /// </summary>
    /// <param name="webPage">The web page.</param>
    /// <param name="strText">The Status Message</param>
    /// <param name="seviority">Error Severity</param>
    public static void ShowStatusMessage(System.Web.UI.Page webpage, string message, int severity)
    {
        switch (severity)
        {
            case 0: //success
                ((HtmlGenericControl)webpage.Master.FindControl("divStatusMessage")).InnerText = message;
                ((HtmlGenericControl)webpage.Master.FindControl("divStatusMessage")).Attributes.Add("style", "background-image: url(../App_Themes/Default/images/master/success.png);background-repeat: no-repeat;background-position: 10px center;background-color: #DFF2BF;border: 1px dotted #4F8A10;margin: 5px 10px 1px 10px;padding: 0px 10px 0px 30px;font: 600 8pt Segoe UI, Tahoma;color: Green;vertical-align: middle; text-align: left;");
                break;
            case 1: //error
                ((HtmlGenericControl)webpage.Master.FindControl("divStatusMessage")).InnerText = message;
                ((HtmlGenericControl)webpage.Master.FindControl("divStatusMessage")).Attributes.Add("style", "background-image: url(../App_Themes/Default/images/master/error.png);background-repeat: no-repeat;background-position: 10px center;background-color:  #FFBABA;border: 1px dotted #D8000C;margin: 5px 10px 1px 10px;padding: 0px 10px 0px 30px;font: 600 8pt Segoe UI, Tahoma;color: Red;vertical-align: middle; text-align: left;");
                break;
            case -1: //warning
                ((HtmlGenericControl)webpage.Master.FindControl("divStatusMessage")).InnerText = message;
                ((HtmlGenericControl)webpage.Master.FindControl("divStatusMessage")).Attributes.Add("style", "background-image: url(../App_Themes/Default/images/master/warning.png);background-repeat: no-repeat;background-position: 10px center;background-color:  #BDE5F8;border: 1px dotted #00529B;margin: 5px 10px 1px 10px;padding: 0px 10px 0px 30px;font: 600 8pt Segoe UI, Tahoma;color: #00529B;vertical-align: middle;text-align: left;");
                break;
            default:
                ((HtmlGenericControl)webpage.Master.FindControl("divStatusMessage")).InnerText = "";
                ((HtmlGenericControl)webpage.Master.FindControl("divStatusMessage")).Attributes.Add("style", "display:none");
                break;
        }
    }

    public static void ClearStatusMessage(System.Web.UI.Page webPage)
    {
        ((HtmlGenericControl)webPage.Master.FindControl("divStatusMessage")).InnerText = "";
        ((HtmlGenericControl)webPage.Master.FindControl("divStatusMessage")).Attributes.Add("style", "display:none");
    }

    /// <summary>
    /// To display the status messages individually in specific screens in the generic format
    /// </summary>
    /// <param name="message">status message</param>
    /// <param name="severity">error severity</param>
    /// <param name="lblValue">label id</param>
    public static void ShowInPageStatus(string message, int severity, Label label)
    {
        label.Width = System.Web.UI.WebControls.Unit.Percentage(99.7);
        switch (severity)
        {
            case 0: //success
                label.Text = message;
                label.Attributes.Add("style", "background-image: url(../css/images/master/success.png);background-repeat: no-repeat;background-position:5px center;background-color: #DFF2BF;border: 1px dotted #4F8A10;margin: 5px 0px 1px 0px;padding: 2px 0px 2px 0px;font: 600 8pt Segoe UI, Tahoma;color: Green;vertical-align: middle; text-align: left; text-indent:25px; display:block;float:center;");
                break;
            case 1: //error
                label.Text = message;
                label.Attributes.Add("style", "background-image: url(../css/images/master/error.png);background-repeat: no-repeat;background-position: 5px center;background-color:  #FFBABA;border: 1px dotted #D8000C;margin: 5px 0px 1px 0px;padding: 2px 0px 2px 0px;font: 600 8pt Segoe UI, Tahoma;color: Red;vertical-align: middle; text-align: left;text-indent:25px; display:block;float:center;");
                break;
            case -1: //warning
                label.Text = message;
                label.Attributes.Add("style", "background-image: url(../css/images/master/warning.png);background-repeat: no-repeat;background-position: 5px center;background-color:  #BDE5F8;border: 1px dotted #00529B;margin: 5px 0px 1px 0px;padding: 2px 0px 2px 0px;font: 600 8pt Segoe UI, Tahoma;color: #00529B;vertical-align: middle;text-align: left;text-indent:25px; display:block;float:center;");
                break;
            default:
                label.Text = "";
                label.Attributes.Add("style", "display:none");
                break;
        }
    }

    /// <summary>
    /// To display the status messages individually in specific screens in the generic format
    /// </summary>
    /// <param name="message">status message</param>
    /// <param name="severity">error severity</param>
    /// <param name="lblValue">label id</param>
    /// <param name="width">Width of the label in Percentage</param>
    public static void ShowInPageStatus(string message, int severity, Label label, Unit widthInPercentage)
    {
        label.Width = widthInPercentage;
        switch (severity)
        {
            case 0: //success
                label.Text = message;
                label.Attributes.Add("style", "background-image: url(../css/images/master/success.png);background-repeat: no-repeat;background-position:5px center;background-color: #DFF2BF;border: 1px dotted #4F8A10;margin: 5px 0px 1px 0px;padding: 2px 0px 2px 0px;font: 600 8pt Segoe UI, Tahoma;color: Green;vertical-align: middle; text-align: left; text-indent:25px; display:block;float:center;");
                break;
            case 1: //error
                label.Text = message;
                label.Attributes.Add("style", "background-image: url(../css/images/master/error.png);background-repeat: no-repeat;background-position: 5px center;background-color:  #FFBABA;border: 1px dotted #D8000C;margin: 5px 0px 1px 0px;padding: 2px 0px 2px 0px;font: 600 8pt Segoe UI, Tahoma;color: Red;vertical-align: middle; text-align: left;text-indent:25px; display:block;float:center;");
                break;
            case -1: //warning
                label.Text = message;
                label.Attributes.Add("style", "background-image: url(../css/images/master/warning.png);background-repeat: no-repeat;background-position: 5px center;background-color:  #BDE5F8;border: 1px dotted #00529B;margin: 5px 0px 1px 0px;padding: 2px 0px 2px 0px;font: 600 8pt Segoe UI, Tahoma;color: #00529B;vertical-align: middle;text-align: left;text-indent:25px; display:block;float:center;");
                break;
            default:
                label.Text = "";
                label.Attributes.Add("style", "display:none");
                break;
        }
    }

    public static void ClearInPageStatus(Label label)
    {
        label.Text = "";
        label.Attributes.Add("style", "display:none");
    }  

    
    /// <summary>
    /// Upload File Format Validation
    /// </summary>
    /// <param name="UploadFileFormat">Upload file format</param>
    /// <param name="ValidFileFormats">Valid file format list in commo seperated list(ex: gif,tif)</param>
    /// <returns></returns>
    public static bool IsValidFile(string UploadFileFormat, string FileFormats, string ValidationType)
    {
        bool isvalid = false;
        string[] ValisFileFormatList = FileFormats.Split(new char[] { ',' });
        if (ValidationType == "0")
        {
            foreach (string s in ValisFileFormatList)
            {
                if (UploadFileFormat.ToLower().Replace(".", "") == s)
                {
                    isvalid = true;
                }
            }
        }
        else
        {
            isvalid = true;
            foreach (string s in ValisFileFormatList)
            {
                if (UploadFileFormat.ToLower().Replace(".", "") == s)
                {
                    isvalid = false;
                }
            }
        }
        return isvalid;
    }
    private static bool CreateFolder(string FolderName, string SharePath)
    {
        bool status = false;

        if (!Directory.Exists(SharePath + "\\" + FolderName))
        {
            Directory.CreateDirectory(SharePath + "\\" + FolderName);
            status = true;
        }
        return status;
    }
    public static bool DeleteNetworkPathFile(string SharePath, string UserName, string Password, string Domain)
    {
        bool status = false;

        try
        {

            NetworkFileAccess objFileAccess = new NetworkFileAccess();
            if (objFileAccess.ImpersonateUser(UserName, Domain, Password))
            {
                if (File.Exists(SharePath))
                {
                    File.Delete(SharePath);
                    status = true;
                }
            }
            else
            {
                NetworkPathMap();
                if (File.Exists(SharePath))
                {
                    File.Delete(SharePath);
                    status = true;
                }
            }

        }
        catch (Exception)
        {
            status = false;
        }
        return status;
    }

    public static string NetworkPathMap()
    {
        Process pro = new Process();
        ProcessStartInfo ProInfo = new ProcessStartInfo("cmd.exe", Convert.ToString(ConfigurationManager.AppSettings["NetUse"]));
        ProInfo.RedirectStandardOutput = true;
        ProInfo.CreateNoWindow = true;
        ProInfo.UseShellExecute = false;
        pro.StartInfo = ProInfo;
        pro.Start();
        string result = pro.StandardOutput.ReadToEnd();

        return result;
    }

    public static string RDLCNetworkPathMap(string FileSharePath, string Password, string Domain, string UserName)
    {
        Process pro = new Process();
        ProcessStartInfo ProInfo = new ProcessStartInfo("cmd.exe", @"/c net use v: " + FileSharePath + " " + Password + " /user:" + Domain + @"\" + UserName);
        ProInfo.RedirectStandardOutput = true;
        ProInfo.CreateNoWindow = true;
        ProInfo.UseShellExecute = false;
        pro.StartInfo = ProInfo;
        pro.Start();
        string result = pro.StandardOutput.ReadToEnd();

        return result;
    }

    public static string NetworkFileUpload(string SaveFileName, string SaveFolderName, string SharePath, FileUpload FileUploadControl, string UserName, string Password, string Domain)
    {
        string strtemp = "Success";
        try
        {

            NetworkFileAccess objFileAccess = new NetworkFileAccess();
            if (objFileAccess.ImpersonateUser(UserName, Domain, Password))
            {
                CreateFolder(SaveFolderName, SharePath);
                FileUploadControl.PostedFile.SaveAs(SharePath + SaveFolderName + "\\" + SaveFileName);
            }
            else
            {
                NetworkPathMap();
                try
                {

                    CreateFolder(SaveFolderName, SharePath);
                    FileUploadControl.PostedFile.SaveAs(SharePath + SaveFolderName + "\\" + SaveFileName);

                }
                catch (Exception ex)
                {
                    strtemp = ex.Message;
                }
            }
        }
        catch (Exception ex)
        {
            if (ex.Message.Contains("Could not find a part of the path"))
            {
                strtemp = "Uploading Error - Invalid file path .Please contact administrator";
            }
            else
            {
                strtemp = ex.Message;
            }
        }
        return strtemp;

    }


    public static string QueryEncrypt(string url, string FieldName1, string Value1)
    {
        return url + QueryStringModule.Encrypt(FieldName1 + "=" + Value1);
    }

    public static string QueryEncrypt(string url, string FieldName1, string Value1, string FieldName2, string Value2)
    {
        return url + QueryStringModule.Encrypt(FieldName1 + "=" + Value1 + "&" + FieldName2 + "=" + Value2);
    }

    public static string QueryEncrypt(string url, string FieldName1, string Value1, string FieldName2, string Value2, string value3)
    {
        return url + QueryStringModule.Encrypt(FieldName1 + "=" + Value1 + "&" + FieldName2 + "=" + Value2 + value3);
    }

    public static string QueryEncrypt(string url, string FieldName1, string Value1, string FieldName2, string Value2, string FieldName3, string Value3)
    {
        return url + QueryStringModule.Encrypt(FieldName1 + "=" + Value1 + "&" + FieldName2 + "=" + Value2 + "&" + FieldName3 + "=" + Value3);
    }

    public static string QueryEncrypt(string url, string FieldName1, string Value1, string FieldName2, string Value2, string FieldName3, string Value3, string FieldName4, string Value4)
    {
        return url + QueryStringModule.Encrypt(FieldName1 + "=" + Value1 + "&" + FieldName2 + "=" + Value2 + "&" + FieldName3 + "=" + Value3 + "&" + FieldName4 + "=" + Value4);
    }
    public static string QueryEncrypt(string url, string FieldName1, string Value1, string FieldName2, string Value2, string FieldName3, string Value3, string FieldName4, string Value4, string FieldName5, string Value5)
    {
        return url + QueryStringModule.Encrypt(FieldName1 + "=" + Value1 + "&" + FieldName2 + "=" + Value2 + "&" + FieldName3 + "=" + Value3 + "&" + FieldName4 + "=" + Value4 + "&" + FieldName5 + "=" + Value5);
    }

    public static string QueryEncrypt(string url, string FieldName1, string Value1, string FieldName2, string Value2, string FieldName3, string Value3, string FieldName4, string Value4, string FieldName5, string Value5, string FieldName6, string Value6)
    {
        return url + QueryStringModule.Encrypt(FieldName1 + "=" + Value1 + "&" + FieldName2 + "=" + Value2 + "&" + FieldName3 + "=" + Value3 + "&" + FieldName4 + "=" + Value4 + "&" + FieldName5 + "=" + Value5 + "&" + FieldName6 + "=" + Value6);
    }
    public static string QueryEncrypt(string url, string FieldName1, string Value1, string FieldName2, string Value2, string FieldName3, string Value3, string FieldName4, string Value4, string FieldName5, string Value5, string FieldName6, string Value6, string FieldName7, string Value7)
    {
        return url + QueryStringModule.Encrypt(FieldName1 + "=" + Value1 + "&" + FieldName2 + "=" + Value2 + "&" + FieldName3 + "=" + Value3 + "&" + FieldName4 + "=" + Value4 + "&" + FieldName5 + "=" + Value5 + "&" + FieldName6 + "=" + Value6 + "&" + FieldName7 + "=" + Value7);
    }

    public static string QueryEncrypt(string url, string FieldName1, string Value1, string FieldName2, string Value2, string FieldName3, string Value3, string FieldName4, string Value4, string FieldName5, string Value5, string FieldName6, string Value6, string FieldName7, string Value7, string FieldName8, string Value8, string FieldName9, string Value9)
    {
        return url + QueryStringModule.Encrypt(FieldName1 + "=" + Value1 + "&" + FieldName2 + "=" + Value2 + "&" + FieldName3 + "=" + Value3 + "&" + FieldName4 + "=" + Value4 + "&" + FieldName5 + "=" + Value5 + "&" + FieldName6 + "=" + Value6 + "&" + FieldName7 + "=" + Value7 + "&" + FieldName8 + "=" + Value8 + "&" + FieldName9 + "=" + Value9);
    }  

    #endregion

    #region masters
    public const System.String USERALREADYEXISTS = "User Already Exists";
    #endregion

    #region Transaction
    #endregion

}